package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaohei.crm.common.exception.XiaoheiException;
import com.xiaohei.crm.common.result.Result;
import com.xiaohei.crm.dao.Inventory;
import com.xiaohei.crm.dao.Material;
import com.xiaohei.crm.dao.Region;
import com.xiaohei.crm.mapper.InventoryMapper;
import com.xiaohei.crm.mapper.MaterialMapper;
import com.xiaohei.crm.mapper.OrdersMapper;
import com.xiaohei.crm.mapper.RegionMapper;
import com.xiaohei.crm.service.InventoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.xiaohei.crm.common.RedisEnums.*;
import static com.xiaohei.crm.common.result.ResultCodeEnum.DATA_ERROR;


@Service
@RequiredArgsConstructor
public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory> implements InventoryService {

    private final InventoryMapper inventoryMapper;
    private final StringRedisTemplate stringRedisTemplate;
    private final OrdersMapper ordersMapper;

    private final RegionMapper regionMapper;

    private final MaterialMapper materialMapper;

    @Override
    public Result<IPage> findByPage(Integer pageIndex, Integer pageSize, String equipmentName, String areaName) {
        Page<Inventory> page = new Page<>(pageIndex, pageSize);
        LambdaQueryWrapper<Inventory> eq = Wrappers.lambdaQuery(Inventory.class).eq(Inventory::getDelFlag, 0);

        if(equipmentName != null){
            List<Integer> ids = new ArrayList<>();
            for (Material material : materialMapper.selectList(Wrappers.lambdaQuery(Material.class).like(Material::getEquipmentName, equipmentName))) {
                ids.add(material.getId());
            }
            if (ids.size() != 0){
                eq.in(Inventory::getMaterialId, ids);
            }else {
                eq.in(Inventory::getMaterialId, -1);
            }
        }

        if(areaName != null){
            List<Long> ids = new ArrayList<>();
            for (Region region : regionMapper.selectList(Wrappers.lambdaQuery(Region.class).like(Region::getArea, areaName))) {
                ids.add(region.getId());
            }
            if (ids.size() != 0){
                eq.in(Inventory::getInventoryArea, ids);
            }else {
                eq.in(Inventory::getInventoryArea, -1);
            }
        }

        Page<Inventory> inventoryPage = inventoryMapper.selectPage(page, eq);
        for (Inventory record : inventoryPage.getRecords()) {
            Region region = regionMapper.selectById(record.getInventoryArea());
            if (region != null) {
                record.setInventoryAreaName(region.getArea());
            }
            Material material = materialMapper.selectById(record.getMaterialId());
            if (material != null) {
                record.setMaterialName(material.getEquipmentName());
            }
        }
        return Result.ok(inventoryPage);
    }

    @Override
    public void saveInventory(Inventory inventory) {
        //判断货物与地区是否一致
        LambdaQueryWrapper<Inventory> queryWrapper = Wrappers
                .lambdaQuery(Inventory.class)
                .eq(Inventory::getMaterialId, inventory.getMaterialId())
                .eq(Inventory::getInventoryArea, inventory.getInventoryArea()).eq(Inventory::getDelFlag, 0);

        Inventory previousInventory = inventoryMapper.selectOne(queryWrapper);

        if (previousInventory != null) {
            //判断入库出库
            if(inventory.getInventoryStatus() == 1) {
                previousInventory.setMaterialNumber(previousInventory.getMaterialNumber() + inventory.getMaterialNumber());
            } else if (previousInventory.getMaterialNumber() - inventory.getMaterialNumber() >= 0){
                previousInventory.setMaterialNumber(previousInventory.getMaterialNumber() - inventory.getMaterialNumber());
            } else {
                throw new XiaoheiException(DATA_ERROR);
            }
            inventoryMapper.update(previousInventory, queryWrapper);
        } else {
            if(inventory.getInventoryStatus() == 1) {
                inventoryMapper.insert(inventory);
            } else {
                throw new XiaoheiException(DATA_ERROR);
            }
        }
    }


    @Override
    public void removeByInventoryId(String id) {
        LambdaUpdateWrapper<Inventory> updateWrapper =
                Wrappers.lambdaUpdate(Inventory.class)
                        .eq(Inventory::getInventoryId, id);
        Inventory inventory = Inventory.builder().delFlag(1).build();
        int update = baseMapper.update(inventory, updateWrapper);

        if (update < 1) {
            throw new XiaoheiException(DATA_ERROR);
        }
    }

}
